package com.daqian.chance.example.admin.modules.member.controller;

import com.daqian.chance.common.base.BaseController;
import com.daqian.chance.core.rest.RestResult;
import com.daqian.chance.example.admin.modules.member.po.dto.MemberDTO;
import com.daqian.chance.example.admin.modules.member.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户-信息 前端控制器
 * </p>
 *
 * @author daqian
 * @since 2019-07-25
 */
@RestController
@RequestMapping("/member")
public class MemberController extends BaseController {

    @Autowired
    private IMemberService iMemberService;


    /**
     * 分页查询 用户-信息
     *
     * @author daqian
     */
    @GetMapping("page")
    public RestResult page(@RequestBody MemberDTO memberDTO) {
        return RestResult.ok(iMemberService.page(memberDTO));
    }

    /**
     * 根据id查询 用户-信息
     *
     * @author daqian
     */
    @GetMapping("info/{memberId}")
    public RestResult info(@PathVariable("memberId") Long memberId) {
        return RestResult.ok(iMemberService.getById(memberId));
    }

    /**
     * 保存 用户-信息
     *
     * @author daqian
     */
    @PostMapping("save")
    public RestResult save(@RequestBody MemberDTO memberDTO) {
        iMemberService.save(memberDTO.convertToEntity());
        return RestResult.ok();
    }

    /**
     * 更新 用户-信息
     *
     * @author daqian
     */
    @PostMapping("update/{memberId}")
    public RestResult remove(@PathVariable("memberId") Long memberId, @RequestBody MemberDTO memberDTO) {
        iMemberService.updateById(memberDTO.convertToEntity().setId(memberId));
        return RestResult.ok();
    }

    /**
     * 根据id删除 用户-信息
     *
     * @author daqian
     */
    @DeleteMapping("{memberId}")
    public RestResult delete(@PathVariable("memberId") Long memberId) {
        iMemberService.removeById(memberId);
        return RestResult.ok();
    }

}
